package com.mdp.cloud.service;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.consul.serviceregistry.ConsulRegistration;
import org.springframework.cloud.consul.serviceregistry.ConsulServiceRegistry;
import org.springframework.context.event.ContextClosedEvent;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;

@Component
public class ServiceCloseUnRegisterService {
    @Autowired(required = false)
    private ConsulServiceRegistry consulServiceRegistry;
    @Autowired
    private ConsulRegistration reg;

    @EventListener(ContextClosedEvent.class)
    public void doUnRegister(ContextClosedEvent contextClosedEvent){
        if(null!=consulServiceRegistry){
            consulServiceRegistry.deregister(reg);
        }

    }
}